/*    
 * Copyright (c) 2013 ISoftStone, Inc.
 * All right reserved.
 *
 * 文件名：      BillingCycleServiceImpl.java
 * 作者:     Jacky Wang
 * 创建日期： 2013-7-29 下午01:46:00
 * 版本：           
 *
 */
package com.isoftstone.industry.aaa.core.service.assist.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.isoftstone.industry.persistance.bean.BillCycleConfig;
import com.isoftstone.industry.persistance.bean.BillingCycle;
import com.isoftstone.industry.aaa.core.dao.assist.BillingCycleDao;
import com.isoftstone.industry.aaa.core.service.assist.IBillingCycleService;
import com.isoftstone.modules.core.persistence.IGenericDao;
import com.isoftstone.modules.core.service.impl.GenericServiceImpl;
import com.isoftstone.modules.core.utils.DateUtils;

/**
 * @author jacky
 */
@Service
public class BillingCycleServiceImpl extends GenericServiceImpl<BillingCycle, String> implements IBillingCycleService
{
	@Resource
	private BillingCycleDao	billingCycleDao;

	@Override
	public IGenericDao<BillingCycle, String> getDao()
	{
		return billingCycleDao;
	}

	@Override
	public BillingCycle findOrCreateLastCycleByConfig(BillCycleConfig config)
	{
		//目前都是按月出帐，则每个帐务周期的起始日期及终止日期都是月初及月末
		String name = getBillingCycleName();
		BillingCycle entity = billingCycleDao.findByNameAndBillConfig(name, config.getId());
		if (entity == null)
		{
			entity = new BillingCycle();
			entity.setName(name);
			entity.setBillCycleConfig(config);
			Date today = DateUtils.today();
			Date beginDate = DateUtils.getLastMonthStart(today);
			Date endDate = DateUtils.getLastMonthEnd(today);
			entity.setBeginDate(beginDate);
			entity.setEndDate(endDate);
			Date blockDate = DateUtils.getDayEnd(DateUtils.adjustByDay(beginDate, config.getStartDay() + config.getDurationDay()
					+ config.getDueDurationDays() - 1));
			entity.setBlockDate(blockDate);
			billingCycleDao.save(entity);
		}
		return entity;
	}
	
	private String getBillingCycleName()
	{
		return DateUtils.date2String(DateUtils.getLastMonthEnd(DateUtils.today()), DateUtils.YYYY_MM_PATTERN);
	}

	private String getNextBillingCycleName()
	{
		return DateUtils.formatTimestamp(DateUtils.getCurrentMonthFirstDay(), DateUtils.YYYY_MM_PATTERN);
	}
	
	@Override
	public BillingCycle findOrCreateNextCycleByConfig(BillCycleConfig config)
	{
		//目前都是按月出帐，则每个帐务周期的起始日期及终止日期都是月初及月末
		String name = getNextBillingCycleName();
		BillingCycle entity = billingCycleDao.findByNameAndBillConfig(name, config.getId());
		if (entity == null)
		{
			entity = new BillingCycle();
			entity.setName(name);
			entity.setBillCycleConfig(config);
			Date today = DateUtils.today();
			Date beginDate = DateUtils.getFirstDayOfMonth(today);
			Date endDate = DateUtils.getLastDayOfMonth(today);
			entity.setBeginDate(beginDate);
			entity.setEndDate(endDate);
			Date blockDate = DateUtils.getDayEnd(DateUtils.adjustByDay(beginDate, config.getStartDay() + config.getDurationDay()
					+ config.getDueDurationDays() - 1));
			entity.setBlockDate(blockDate);
			billingCycleDao.save(entity);
		}
		return entity;
	}

	@Override
	public void createBillingCycle4ThisYear(BillCycleConfig config)
	{
		Date today = DateUtils.today();
		String year = DateUtils.formatDate(today, DateUtils.YYYY_PATTERN);
		for (int i = 1; i <= 12; i++)
		{
			String name = year + (i >= 10 ? i : "0" + i);
			BillingCycle entity = billingCycleDao.findByNameAndBillConfig(name, config.getId());
			if (entity == null)
			{
				entity = new BillingCycle();
				entity.setName(name);
				entity.setBillCycleConfig(config);
				Date beginDate = DateUtils.string2Date(name+"01 00:00:00");
				Date endDate = DateUtils.getLastDayOfMonth(beginDate);
				entity.setBeginDate(beginDate);
				entity.setEndDate(endDate);
				Date blockDate = DateUtils.getDayEnd(DateUtils.adjustByDay(beginDate, config.getStartDay() + config.getDurationDay()
						+ config.getDueDurationDays() - 1));
				entity.setBlockDate(blockDate);
				billingCycleDao.save(entity);
			}
		}
	}

}
